<?php

class Db_Locale_Languages extends Common {
	protected static $tableName = TAB_LANGUAGES;
	protected static $dbFields = array(
		'id',
		'l_define',
		'l_name_key',
		'l_short_name_key',
		'l_country_based',
		'deleted'
	);
	public $objectArray;
	public $id;
	public $l_define;
	public $l_name_key;
	public $l_short_name_key;
	public $l_country_based;
	public $deleted;

	function __construct($value=NULL, $fieldName=NULL) {
		parent::__construct($value, $fieldName);
	}

	/**
	 * The function will return in any case all existing languages from the database.
	 * In case the $projectId is not false it will try to also return the default language for this project.
	 * If $projectId is true it will get the default language for the current loaded project.
	 * If $projectId is an integer it will get the default language for that project ID.
	 *
	 * @static
	 * @param bool|int $projectId The id of the project. Pass false to get all languages.
	 * @return mixed
	 */
	public static function getLangs($projectId = true) {
		if (!$projectId) {
			return self::findAllArray();
		} else {
			if ($projectId === true) {
				$projectId = self::$PROJECT['id'];
			}
			$sql = "SELECT `l`.*, `prl`.`prl_is_default` AS `is_default` FROM ?f AS `l`
					LEFT JOIN ?f AS `prl` ON `prl`.`prl_language_id` = `l`.`id` AND `prl`.`prl_project_id` = ?i
					WHERE `l`.`deleted` = 0 ORDER BY `prl`.`prl_is_default` DESC;";
			return self::$DB->select($sql, self::$tableName, TAB_PROJECT_LANGUAGES, $projectId);
		}
	}

	public static function setDefaultLanguage($langId) {
		if ((int)$langId) {
			$sql = "UPDATE ?f l SET `l`.`l_country_based` = IF(`l`.`id` = ?d, 1, 0)";
			return self::$DB->q($sql, TAB_LANGUAGES, $langId);
		} else {
			return false;
		}
	}
}
